package syyrjx.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class ScoreServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.score(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.score(request, response);
    }
    private void score(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        List<Map<String,Object>> questionList = (List) session.getAttribute("questionList");
        double score = 0.0;
        for(Map<String,Object> question :questionList){
            String id = ((Integer) question.get("id")).toString();
            String answer = (String)question.get("answer");
            System.out.println("正确答案"+answer);
            System.out.println("我的答案"+request.getParameter(id));
            if(answer.equals(request.getParameter(id))){
                score+=100/3.0;
                System.out.println("答对了");
            }
        }
        request.setAttribute("score",(int)score);
        request.getRequestDispatcher("scoreShow.jsp").forward(request,response);
    }

}
